<?php 

class GalleryController extends Controller{

    public $layout = '//layouts/main';
    
    public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
    
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','album', 'ajaxgetgallerys'),
				'users'=>array('*'),
			),
            array('allow', // allow admin user to perform 'admin' and 'ajaxset' actions
				'actions'=>array('admin', 'ajaxset'),
				'users'=>array('@'),
                'expression'=>'!Yii::app()->user->isGuest',
			),

			array('allow', // allow admin user to perform 'admin' and 'ajaxset' actions
				'actions'=>array('admin', 'ajaxset', 'picasa'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
    
    public function actionAlbum($id, $title){
        
        if(is_null(PicasaGallery::model()->find('album LIKE :aa', array(':aa'=>$id)))){ // disabled album
            throw new CHttpException(404, 'A galéria nem elérhető');
        }
        
        $this->render('album',array(
            'album'=>$id            
        ));
    }
    
    public function actionIndex(){
        $this->render('index', array(
            'albums'=>PicasaGallery::model()->findAll()
        ));
    }
    
    public function actionAdmin(){
        Yii::app()->theme = 'classic';
        $this->render('admin', array(
            'albums'=>PicasaGallery::model()->findAll()
        ));
    }
    
    public function actionAjaxset(){
        $message = array(
            'status'=>'failed'
        );
        
        $album = Yii::app()->request->getParam('album', false);
                
        $gallery = new PicasaGallery();        
        $gallery = $gallery->find('album LIKE :aa', array(':aa'=>$album));
        
        if(is_null($gallery)){ // ACTIVTE
            
            $gallery = new PicasaGallery();
            $gallery->album = $album;
            
            if($gallery->save() == 1){
                $message['status'] = 'active';
            }
        } else {
            
            if($gallery->delete() == 1){
                $message['status'] = 'hidden';
            }
            
        }
        $message['message'] = $gallery->getErrors();
        
        echo CJSON::encode($message);
    }
    
    public function actionAjaxgetgallerys(){
        echo CJSON::encode(PicasaGallery::model()->findAll());
    }
}